﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Reflection;

namespace A2DFramework.AOPService.Core
{
    public class AOPHelper
    {
        public static T CheckDefinationTag<T>(System.Runtime.Remoting.Messaging.IMessage requestMsg, ref bool tagExists)
            where T : BaseAOPAttribute
        {
            IMethodCallMessage methodCall = (IMethodCallMessage)requestMsg;
            object[] attrs = methodCall.MethodBase.GetCustomAttributes(typeof(T), false);
            if (attrs == null || attrs.Count() <= 0)
            {
                tagExists = false;
                return null;
            }
            T attr = attrs[0] as T;
            if (attr == null)
            {
                tagExists = false;
                return null;
            }
            return attr;
        }

        public static T CheckDefinationTag<T>(MethodBase methodBase, ref bool tagExists)
            where T : Attribute
        {
            object[] attrs = methodBase.GetCustomAttributes(typeof(T), false);
            if (attrs == null || attrs.Count() <= 0)
            {
                tagExists = false;
                return null;
            }
            T attr = attrs[0] as T;
            if (attr == null)
            {
                tagExists = false;
                return null;
            }
            return attr;
        }
    }
}
